/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.om.dto.RouteDto;
import com.excesys.exsecs.om.entity.OmICheckRoute;
import com.excesys.exsecs.om.entity.OmIRoutePointRel;
import com.excesys.exsecs.om.mapper.OmICheckRouteMapper;
import com.excesys.exsecs.om.mapper.OmIRoutePointRelMapper;
import com.excesys.exsecs.om.service.OmICheckRouteService;
import com.excesys.exsecs.om.vo.RouteVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 巡检路线表
 *
 * @author weiyj
 * @date 2023-03-01 14:10:58
 */
@Service
@RequiredArgsConstructor
public class OmICheckRouteServiceImpl extends ServiceImpl<OmICheckRouteMapper, OmICheckRoute>
		implements OmICheckRouteService {

	private final OmIRoutePointRelMapper omIRoutePointRelMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveRoute(RouteDto routeDto) {

		OmICheckRoute omICheckRoute = new OmICheckRoute();
		BeanUtils.copyProperties(routeDto, omICheckRoute);
		baseMapper.insert(omICheckRoute);
		// 保存用户岗位信息
		Optional.ofNullable(routeDto.getRelList()).ifPresent(relList -> {
			relList.stream().map(omIRoutePointRel -> {
				omIRoutePointRel.setRouteId(omICheckRoute.getIrId());
				return omIRoutePointRel;
			}).forEach(omIRoutePointRelMapper::insert);
		});
		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R<Boolean> updateRoute(RouteDto RouteDto) {

		OmICheckRoute omICheckRoute = new OmICheckRoute();
		BeanUtils.copyProperties(RouteDto, omICheckRoute);
		omICheckRoute.setUpdateTime(LocalDateTime.now());
		this.updateById(omICheckRoute);
		omIRoutePointRelMapper
				.delete(Wrappers.<OmIRoutePointRel>lambdaQuery().eq(OmIRoutePointRel::getRouteId, RouteDto.getIrId()));
		// 保存用户岗位信息
		Optional.ofNullable(RouteDto.getRelList()).ifPresent(relLists -> {
			relLists.stream().map(omIRoutePointRel -> {
				omIRoutePointRel.setRouteId(omICheckRoute.getIrId());
				return omIRoutePointRel;
			}).forEach(omIRoutePointRelMapper::insert);
		});
		return R.ok();
	}

	@Override
	public Boolean removeRouteById(OmICheckRoute omICheckRoute) {
		omIRoutePointRelMapper.delete(
				Wrappers.<OmIRoutePointRel>lambdaQuery().eq(OmIRoutePointRel::getRouteId, omICheckRoute.getIrId()));
		this.removeById(omICheckRoute.getIrId());
		return Boolean.TRUE;
	}

	/**
	 * 分页查询用户信息（含有角色信息）
	 * @param page 分页对象
	 * @param routeDto 参数列表
	 * @return
	 */
	@Override
	public IPage<RouteVo> getRouteWithRelPage(Page page, RouteDto routeDto) {
		return baseMapper.getRouteWithRelPage(page, routeDto);
	}

}
